#pragma once
#ifndef __SWIG__
#include <D3D11.h>
#include <D3D10.h>
#include <D2D1.h>
#include <DWrite.h>
#include <wincodec.h>
#include <map>
#endif //#ifndef __SWIG__
#include "CComm.h"

#ifdef __SWIG__
#include "CDXDevice_swig.h"
%include <typemaps.i>

%apply UINT& OUTPUT {UINT& nBits};
%apply UINT& OUTPUT {UINT& nChannel};
%apply BOOL& OUTPUT {BOOL& bAvgBits};
%apply SWIGTYPE** OUTPUT {IDWriteTextFormat** textFormat}
%apply SWIGTYPE** OUTPUT {IDXGIFactory1** ppFactory}

#endif

class CDXDevice : public CRefCount
{
	
protected:
	ID3D11Device*            m_pD3D11Device;
	ID3D11DeviceContext*     m_pD3D11DeviceContext;
	CCritSec* m_csDeviceContext;
	//D3D10 Device, for D3D11 can't interop with D2D yet.
	ID3D10Device* m_pD3D10Device;
	IDXGIFactory1* m_pDXGIFactory1;
	/////////////////////////////////////////////////////
	ID2D1Factory* m_pD2DFactory;
	IDWriteFactory* m_pDWriteFactory;
	///////////for Save Image File
	IWICImagingFactory *m_pWICFactory;
private:
	HRESULT _CreateD3DDevice();
protected:
	D3D_FEATURE_LEVEL m_FeatureLevelsSupported;
public:
#ifndef __SWIG__
	virtual HRESULT GetWICFactory(IWICImagingFactory** pWICFactory);
#endif
	
	  
	
public:
	CDXDevice();
	virtual ~CDXDevice();
	virtual CCritSec* GetContextLock() { return m_csDeviceContext;};
	virtual ID3D11Device* GetD3D11Device();
	virtual ID3D11DeviceContext* GetD3D11DeviceContext();
	#ifndef __SWIG__
	virtual HRESULT GetD2DFactory(ID2D1Factory** ppD2DFactory);
	virtual HRESULT GetDWriteFactory(IDWriteFactory** ppDWriteFactory);
	virtual HRESULT GetD3D10Device(ID3D10Device** ppDevice);

	virtual HRESULT GetDXGIAdapter(UINT idx, IDXGIAdapter** ppAdapter);
	virtual HRESULT GetDXGIFactory(IDXGIFactory1** ppDXGIFactory);
	#endif
	static HRESULT GetWICFmtFromDXGIFmt(DXGI_FORMAT dxgiFmt, GUID* wicFmt);

	D3D_FEATURE_LEVEL GetFeatureLevelSupported();
	virtual HRESULT MakeWindowAssociation(int hwnd, UINT flag);
	static HRESULT GetDXGIFormatNumBits(DXGI_FORMAT fmt, UINT& nBits);
	static HRESULT GetDXGIFormatNumChannel(DXGI_FORMAT fmt, UINT& nChannel, BOOL& bAvgBits);

#ifndef __SWIG__
	virtual HRESULT CreateTextFormat(IDWriteTextFormat ** textFormat, FLOAT fontSize = 14, const WCHAR * fontFamilyName = L"Arial",  IDWriteFontCollection * fontCollection = NULL,
		DWRITE_FONT_WEIGHT  fontWeight = DWRITE_FONT_WEIGHT_REGULAR, DWRITE_FONT_STYLE  fontStyle = DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH  fontStretch = DWRITE_FONT_STRETCH_NORMAL,
		const WCHAR * localeName = L"en-us");
#else
	virtual HRESULT CreateTextFormat(IDWriteTextFormat** textFormat, FLOAT fontSize = 14, const WCHAR * fontFamilyName = "Arial",  IDWriteFontCollection * fontCollection = NULL,
		DWRITE_FONT_WEIGHT  fontWeight = DWRITE_FONT_WEIGHT_REGULAR, DWRITE_FONT_STYLE  fontStyle = DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH  fontStretch = DWRITE_FONT_STRETCH_NORMAL,
		const WCHAR * localeName = "en-us");
#endif
#ifndef __SWIG__
	virtual HRESULT CreateTextLayoutEx(IDWriteTextLayout ** textLayout, const WCHAR* str, UINT len,
		  FLOAT maxWidth, FLOAT maxHeight, IDWriteTextFormat* textFormat);
	virtual HRESULT CreateTextLayout(IDWriteTextLayout ** textLayout, const WCHAR* str, UINT len,
	 	  FLOAT maxWidth, FLOAT  maxHeight, FLOAT fontSize);
	virtual HRESULT CreateGeometryGroup(D2D1_FILL_MODE fillMode, ID2D1Geometry **geometries, UINT geometriesCount, ID2D1GeometryGroup **geometryGroup);
	virtual HRESULT CreateEllipseGeometry( const D2D1_ELLIPSE &ellipse, ID2D1EllipseGeometry **ellipseGeometry);
	virtual HRESULT CreatePathGeometry(ID2D1PathGeometry **pathGeometry);
	virtual HRESULT CreateRectangleGeometry( const D2D1_RECT_F *rectangle, ID2D1RectangleGeometry **rectangleGeometry);
	virtual HRESULT CreateDrawingStateBlock(ID2D1DrawingStateBlock **drawingStateBlock);
#endif
	/////////for swig, since swig can't get pointer of pointer from parameter
	virtual ID2D1Factory* GetD2DFactory();
	virtual IDWriteFactory* GetDWriteFactory();
	virtual ID3D10Device* GetD3D10Device();

	virtual IDXGIAdapter* GetDXGIAdapter(UINT idx);
	virtual IDXGIFactory1* GetDXGIFactory();

	virtual IDWriteTextLayout* CreateTextLayoutEx(const WCHAR* str, UINT len,
		FLOAT maxWidth, FLOAT maxHeight, IDWriteTextFormat* textFormat);
	virtual IDWriteTextLayout* CreateTextLayout( const WCHAR* str, UINT len,
		FLOAT maxWidth, FLOAT  maxHeight, FLOAT fontSize);
	virtual ID2D1EllipseGeometry* CreateEllipseGeometry( const D2D1_ELLIPSE &ellipse);
	virtual ID2D1PathGeometry* CreatePathGeometry();
	virtual ID2D1RectangleGeometry* CreateRectangleGeometry(D2D1_RECT_F rectangle);
	virtual ID2D1DrawingStateBlock* CreateDrawingStateBlock();
};

class CDXBase 
{
protected:
	CDXDevice* m_pDevice;
public:
	CDXBase(CDXDevice* pDevice);
	virtual ~CDXBase();
	virtual BOOL IsSameDevice(const CDXBase* pOther) const;
	virtual CDXDevice* GetDevive() const { return m_pDevice;} ;
	virtual CCritSec* GetContextLock() { return m_pDevice->GetContextLock();}  
	
};
